package org.come.Jpanel;

import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.List;
import java.util.Vector;

import javax.swing.BorderFactory;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.ScrollPaneConstants;
import javax.swing.SwingConstants;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.JTableHeader;

import org.come.bean.GangResultBean;
import org.come.bean.LoginResult;
import org.come.entity.Gangapplytable;
import org.come.until.*;

import com.tool.btn.FactionBtn;
import com.tool.role.RoleData;
import com.tool.tcpimg.UIUtils;
import com.updateNew.MyIsif;

 

/**
 * 帮派成员面板
 * 
 * @author HGC<br>
 * @time 2020年1月11日 上午11:27:11<br>
 * @class 类名:FactionMemberJpanel<br>
 */
public class FactionMemberJpanel extends JPanel {

    /**
     * 字段名:btnMenuAll 全部成员菜单按钮<br>
     * 字段名:btnMenuCore 核心成员菜单按钮<br>
     * 字段名:btnMenuApply 申请人菜单按钮<br>
     * 字段名:btnOffice 任职按钮<br>
     * 字段名:btnOutgoing 卸任按钮<br>
     * 字段名:btnKick 踢出帮派按钮<br>
     * 字段名:btnBreak 脱离帮派按钮<br>
     * 字段名:btnAddFriend 添加好友按钮<br>
     */
    private FactionBtn btnMenuAll, btnMenuCore, btnMenuApply, btnOffice, btnOutgoing, btnKick, btnBreak, btnAddFriend;

    /** 全部成员5 核心成员6 申请人7 */
    private int menuType = 5;

    private JTextField textField;

    private JTable table;
    private DefaultTableModel tableModel;

    private JScrollPane scrollPane;

    /**
     * 字段名:labCheck 勾选框<br>
     */
    private JLabel labCheck;

    private FactionCardJpanel factionCardJpanel;

    /**
     * 字段名:olineNum 在线人数<br>
     */
    private int olineNum;

    public FactionMemberJpanel(FactionCardJpanel factionCardJpanel) {
        this.factionCardJpanel = factionCardJpanel;
        setPreferredSize(new Dimension(662, 475));
        setOpaque(false);
        setLayout(null);

        getBtnMenuAll();
        getBtnMenuApply();
        getBtnMenuCore();
        getTextField();
        getScrollPane();
        getBtnOffice();
        getBtnOutgoing();
        getBtnKick();
        getBtnBreak();
        getBtnAddFriend();
        getLabCheck();
    }

    private ImageIcon icon;
    private ImageIcon iconCoumn;

    /** 展示不同的表格 */
    public void changeTable() {
        if (menuType < 7) {
            tableModel.setColumnCount(6);
            table.getColumnModel().getColumn(0).setPreferredWidth(110);
            table.getColumnModel().getColumn(1).setPreferredWidth(90);
            table.getColumnModel().getColumn(2).setPreferredWidth(105);
            table.getColumnModel().getColumn(3).setPreferredWidth(80);
            table.getColumnModel().getColumn(4).setPreferredWidth(120);
            table.getColumnModel().getColumn(5).setPreferredWidth(65);
        } else if (menuType == 7) {
            tableModel.setColumnCount(3);
            table.getColumnModel().getColumn(0).setPreferredWidth(190);
            table.getColumnModel().getColumn(1).setPreferredWidth(190);
            table.getColumnModel().getColumn(2).setPreferredWidth(190);
        }
    }

    /**
     * 是否为重要成员
     */
    public boolean Important(String post) {
        if (post == null) {
            return false;
        }
        if ("帮主".equals(post) || "护法".equals(post)) {
            return true;
        }
        return false;
    }

    /**
     * 是否为帮主
     */
    public boolean ImportantWang(String post) {
        if (post == null) {
            return false;
        }
        if ("帮主".equals(post)) {
            return true;
        }
        return false;
    }

    /** 展示数据 */
    public void showMenuMessage(GangResultBean gangResultBean) {
        tableModel.setRowCount(0);
        olineNum = 0;
        if (gangResultBean == null) {
            return;
        }
        if (menuType == 5) {
            List<LoginResult> roleTables = gangResultBean.getRoleTables();
            Icon icon2 = labCheck.getIcon();
            for (int i = 0; i < roleTables.size(); i++) {
                LoginResult loginResult = roleTables.get(i);
                if (icon2 != null) {
                    if (!"在线".equals(loginResult.getUptime())) {
                        continue;
                    }
                }
                Vector<String> rowData = new Vector<>();
                rowData.add(loginResult.getRolename());
                rowData.add(loginResult.getGangpost());
                rowData.add(loginResult.getRace_name());
                rowData.add(AnalysisString.lvl(loginResult.getGrade()));
                rowData.add(loginResult.getContribution().toString());
                if (!"在线".equals(loginResult.getUptime())) {
                    rowData.add(MessagrFlagUntil.timeStamp2Date(new Long(loginResult.getUptime()), "yy-MM-dd"));
                } else {
                    olineNum++;
                    rowData.add(loginResult.getUptime());
                }
                tableModel.addRow(rowData);
            }
        } else if (menuType == 6) {
            List<LoginResult> roleTables = gangResultBean.getRoleTables();
            Icon icon2 = labCheck.getIcon();
            for (int i = 0; i < roleTables.size(); i++) {
                LoginResult loginResult = roleTables.get(i);
                if (icon2 != null) {
                    if (!"在线".equals(loginResult.getUptime())) {
                        continue;
                    }
                }
                if (Important(loginResult.getGangpost())) {
                    Vector<String> rowData = new Vector<>();
                    rowData.add(loginResult.getRolename());
                    rowData.add(loginResult.getGangpost());
                    rowData.add(loginResult.getRace_name());
                    rowData.add(AnalysisString.lvl(loginResult.getGrade()));
                    rowData.add(loginResult.getContribution().toString());
                    if (!"在线".equals(loginResult.getUptime())) {
                        rowData.add(MessagrFlagUntil.timeStamp2Date(new Long(loginResult.getUptime()), "yy-MM-dd"));
                    } else {
                        olineNum++;
                        rowData.add(loginResult.getUptime());
                    }
                    tableModel.addRow(rowData);
                }
            }
        } else if (menuType == 7) {
            List<Gangapplytable> gangapplytables = gangResultBean.getGangapplytables();
            for (int i = 0; i < gangapplytables.size(); i++) {
                Gangapplytable gangapplytable = gangapplytables.get(i);
                Vector<String> rowData = new Vector<>();
                rowData.add(gangapplytable.getRolename());
                rowData.add(gangapplytable.getRace_name());
                rowData.add(AnalysisString.lvl(gangapplytable.getGrade().intValue()));
                tableModel.addRow(rowData);
            }
        }
    }

    /** 展示不同职位不同的按钮 */
    public void showBtn(boolean type) {
        if("水墨UI".equals(MyIsif.getStyle())) {
            try {
                LoginResult loginResult = RoleData.getRoleData().getLoginResult();
                if (menuType < 7) {
                    if (type) {
                        if (ImportantWang(loginResult.getGangpost())) {
                            btnOffice.setBounds(204, 429, 59, 24);
                            btnOutgoing.setText("卸任");
                            btnOutgoing.setIcons(CutButtonImage.cuts("skin/8100226/newbutton/B20.png"));
                            btnOutgoing.setBounds(267, 429, 59, 24);
                            btnBreak.setText("脱离帮派");
                            btnKick.setText("踢出帮派");
                            btnKick.setVisible(type);
                            btnOffice.setVisible(type);
                            btnOutgoing.setVisible(type);
                        } else if (Important(loginResult.getGangpost())) {
                            // btnOffice.setBounds(305, 429, 60, 26);
                            // btnOutgoing.setText("卸任");
                            // btnOutgoing.setIcons(CutButtonImage.cuts("skin/8100226/button/32.png"));
                            // btnOutgoing.setBounds(368, 429, 60, 26);
                            btnBreak.setText("脱离帮派");
                            // btnOffice.setVisible(type);
                            // btnOutgoing.setVisible(type);
                        }
                    } else {
                        btnKick.setVisible(type);
                        btnOffice.setVisible(type);
                        btnOutgoing.setVisible(type);
                    }
                } else if (menuType == 7) {
                    if (type) {
                        if (Important(loginResult.getGangpost())) {
                            btnOutgoing.setText("拒绝玩家");
                            btnOutgoing.setIcons(CutButtonImage.cuts("skin/8100226/newbutton/B22.png"));
                            btnOutgoing.setBounds(227, 429, 99, 24);
                            btnBreak.setText("清空列表");
                            btnKick.setText("接收玩家");
                            btnKick.setVisible(type);
                            btnOutgoing.setVisible(type);
                        }
                    } else {
                        btnKick.setVisible(type);
                        btnOutgoing.setVisible(type);
                    }
                }
            } catch (Exception e) {
                e.printStackTrace();
            }

        }else{
            try {
                LoginResult loginResult = RoleData.getRoleData().getLoginResult();
                if (menuType < 7) {
                    if (type) {
                        if (ImportantWang(loginResult.getGangpost())) {
                            btnOffice.setBounds(204, 429, 60, 26);
                            btnOutgoing.setText("卸任");
                            btnOutgoing.setIcons(CutButtonImage.cuts("inkImg/hongmu/1/6026.png"));
                            btnOutgoing.setBounds(267, 429, 60, 26);
                            btnBreak.setText("脱离帮派");
                            btnKick.setText("踢出帮派");
                            btnKick.setVisible(type);
                            btnOffice.setVisible(type);
                            btnOutgoing.setVisible(type);
                        } else if (Important(loginResult.getGangpost())) {
                            // btnOffice.setBounds(305, 429, 60, 26);
                            // btnOutgoing.setText("卸任");
                            // btnOutgoing.setIcons(CutButtonImage.cuts("skin/8100226/button/32.png"));
                            // btnOutgoing.setBounds(368, 429, 60, 26);
                            btnBreak.setText("脱离帮派");
                            // btnOffice.setVisible(type);
                            // btnOutgoing.setVisible(type);
                        }
                    } else {
                        btnKick.setVisible(type);
                        btnOffice.setVisible(type);
                        btnOutgoing.setVisible(type);
                    }
                } else if (menuType == 7) {
                    if (type) {
                        if (Important(loginResult.getGangpost())) {
                            btnOutgoing.setText("拒绝玩家");
                            btnOutgoing.setIcons(CutButtonImage.cuts("inkImg/hongmu/1/6026.png"));
                            btnOutgoing.setBounds(227, 429, 100, 26);
                            btnBreak.setText("清空列表");
                            btnKick.setText("接收玩家");
                            btnKick.setVisible(type);
                            btnOutgoing.setVisible(type);
                        }
                    } else {
                        btnKick.setVisible(type);
                        btnOutgoing.setVisible(type);
                    }
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    @Override
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);
        if("水墨UI".equals(MyIsif.getStyle())) {

            if (icon == null) {
                icon = new ImageIcon("inkImg/newbackground/B223.png");
            }
            g.drawImage(icon.getImage(), 0, 0, 662, 475, null);
            if (iconCoumn != null) {
                g.drawImage(iconCoumn.getImage(), 49, 89, 589, 21, null);
            }
        }else{
            if (icon == null) {
                icon = new ImageIcon("inkImg/hongmu/2/S167.png");
            }
            g.drawImage(icon.getImage(), 0, 0, 662, 475, null);
            if (iconCoumn != null) {
                g.drawImage(iconCoumn.getImage(), 49, 89, 589, 21, null);
            }
        }

    }

    public FactionBtn getBtnMenuAll() {
        if (btnMenuAll == null) {
            if("水墨UI".equals(MyIsif.getStyle())) {

                btnMenuAll = new FactionBtn("skin/8100226/button/B273.png", 1, 5, this);
                btnMenuAll.setBounds(56, 63, 96, 26);
            }else{
                btnMenuAll = new FactionBtn("inkImg/hongmu/2/B273.png", 1, 5, this);
                btnMenuAll.setBounds(56, 63, 75, 26);
            }
            this.add(btnMenuAll);
        }
        return btnMenuAll;
    }

    public void setBtnMenuAll(FactionBtn btnMenuAll) {
        this.btnMenuAll = btnMenuAll;
    }

    public FactionBtn getBtnMenuCore() {
        if (btnMenuCore == null) {
            if("水墨UI".equals(MyIsif.getStyle())) {
                btnMenuCore = new FactionBtn("skin/8100226/button/B274.png", 1, 6, this);
                btnMenuCore.setBounds(154, 63, 96, 26);

            }else{
                btnMenuCore = new FactionBtn("inkImg/hongmu/2/B274.png", 1, 6, this);
                btnMenuCore.setBounds(154, 63, 75, 26);
            }
            this.add(btnMenuCore);
        }
        return btnMenuCore;
    }

    public void setBtnMenuCore(FactionBtn btnMenuCore) {
        this.btnMenuCore = btnMenuCore;
    }

    public FactionBtn getBtnMenuApply() {
        if (btnMenuApply == null) {
            if("水墨UI".equals(MyIsif.getStyle())) {
                btnMenuApply = new FactionBtn("skin/8100226/button/B278.png", 1, 7, this);
                btnMenuApply.setBounds(252, 63, 96, 26);

            }else{
                btnMenuApply = new FactionBtn("inkImg/hongmu/2/v291.png", 1, 7, this);
                btnMenuApply.setBounds(252, 63, 75, 26);
            }
            this.add(btnMenuApply);
        }
        return btnMenuApply;
    }

    public void setBtnMenuApply(FactionBtn btnMenuApply) {
        this.btnMenuApply = btnMenuApply;
    }

    public ImageIcon getIcon() {
        return icon;
    }

    public void setIcon(ImageIcon icon) {
        this.icon = icon;
    }

    public ImageIcon getIconCoumn() {
        return iconCoumn;
    }

    public void setIconCoumn(ImageIcon iconCoumn) {
        this.iconCoumn = iconCoumn;
    }

    public int getMenuType() {
        return menuType;
    }

    public void setMenuType(int menuType) {
        this.menuType = menuType;
    }

    public JTextField getTextField() {
        if (textField == null) {
            textField = new JTextField();
            textField.setBounds(540, 69, 94, 16);
            textField.setOpaque(false);
            textField.setBorder(BorderFactory.createEmptyBorder());
            textField.setForeground(Color.white);
            textField.setCaretColor(Color.white);
            textField.setFont(UIUtils.TEXT_FONT1);
            this.add(textField);
        }
        return textField;
    }

    public void setTextField(JTextField textField) {
        this.textField = textField;
    }

    public JTable getTable() {
        if (table == null) {
            table = new JTable() {
                @Override
                public boolean isCellEditable(int row, int column) {
                    return false;
                }
            };

            // 帮派成员信息表格
            table.setShowGrid(false);
            table.setOpaque(false);

            DefaultTableCellRenderer renderer = new DefaultTableCellRenderer() {

                @Override
                public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected,
                        boolean hasFocus, int row, int column) {
                    super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);
                    if (row < olineNum) {
                        setForeground(Color.ORANGE);
                    } else {
                        setForeground(Color.white);
                    }
                    return this;
                }
            };
            renderer.setHorizontalAlignment(SwingConstants.CENTER);
            table.setDefaultRenderer(Object.class, renderer);

            JTableHeader header = table.getTableHeader();

            // 设置表头的高度
            header.setPreferredSize(new Dimension(header.getWidth(), 0));
            table.getTableHeader().setBackground(UIUtils.Color_BACK);
            table.getTableHeader().setVisible(true);
            table.getTableHeader().setBorder(BorderFactory.createLineBorder(Color.ORANGE));

            table.setSelectionBackground(UIUtils.Color_BACK);
            table.setSelectionForeground(Color.white);
            table.setForeground(Color.white);
            table.setFont(UIUtils.TEXT_FONT);
            table.setBackground(UIUtils.Color_BACK);
            table.setModel(getTableModel());

            // 设置列间距590
            changeTable();
            // 设置行宽
            table.setRowHeight(25);
            table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
            table.isCellEditable(1, 1);
            table.setEnabled(true);
        }
        return table;
    }

    public void setTable(JTable table) {
        this.table = table;
    }

    public DefaultTableModel getTableModel() {
        if (tableModel == null) {
            tableModel = new DefaultTableModel();
            Vector<Vector<String>> verVectors = new Vector<Vector<String>>();
            for (int i = 0; i < 50; i++) {
                Vector<String> vector = new Vector<>();
                for (int j = 0; j < 6; j++) {
                    vector.add(String.valueOf("2024-06-01"));
                }
                verVectors.add(vector);
            }
            Vector<String> vector3 = new Vector<String>();
            vector3.add("");
            vector3.add("");
            vector3.add("");
            vector3.add("");
            vector3.add("");
            vector3.add("");
            vector3.add("");
            vector3.add("");
            vector3.add("");
            tableModel.setDataVector(verVectors, vector3);
        }
        return tableModel;
    }

    public void setTableModel(DefaultTableModel tableModel) {
        this.tableModel = tableModel;
    }

    public JScrollPane getScrollPane() {
        if (scrollPane == null) {
            scrollPane = new JScrollPane(getTable());
            if("水墨UI".equals(MyIsif.getStyle())) {

                scrollPane.setBounds(51, 109, 585, 314);
            }else{
                scrollPane.setBounds(30, 109, 610, 314);
            }
            scrollPane.setOpaque(false);
            scrollPane.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
            scrollPane.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
            scrollPane.getVerticalScrollBar().setUI(new ScrollUI());
            scrollPane.getVerticalScrollBar().setUnitIncrement(30);
            scrollPane.setBorder(BorderFactory.createEmptyBorder());
            scrollPane.getViewport().setOpaque(false);
            this.add(scrollPane);
        }


        return scrollPane;
    }

    public void setScrollPane(JScrollPane scrollPane) {
        this.scrollPane = scrollPane;
    }

    public FactionBtn getBtnOffice() {
        if (btnOffice == null) {
            if("水墨UI".equals(MyIsif.getStyle())) {
                btnOffice = new FactionBtn("skin/8100226/newbutton/B20.png", 1, UIUtils.COLOR_BLACK, "任职", UIUtils.TEXT_HY16, 10,
                        this);
                btnOffice.setBounds(204, 429, 59, 24);

            }else{
                btnOffice = new FactionBtn("inkImg/hongmu/1/6026.png", 1, UIUtils.COLOR_BTNPUTONG2, "任职", UIUtils.TEXT_HY88, 10,
                        this);
                btnOffice.setBounds(204, 429, 60, 26);
            }

            this.add(btnOffice);
        }
        return btnOffice;
    }

    public void setBtnOffice(FactionBtn btnOffice) {
        this.btnOffice = btnOffice;
    }

    public FactionBtn getBtnOutgoing() {
        if (btnOutgoing == null) {
            if("水墨UI".equals(MyIsif.getStyle())) {
                btnOutgoing = new FactionBtn("skin/8100226/newbutton/B20.png", 1, UIUtils.COLOR_WHITE2, "卸任", UIUtils.TEXT_HY16,
                        11, this);
                btnOutgoing.setBounds(267, 429, 59, 24);

            }else{
                btnOutgoing = new FactionBtn("inkImg/hongmu/1/6026.png", 1, UIUtils.COLOR_BTNPUTONG2, "卸任", UIUtils.TEXT_HY88,
                        11, this);
                btnOutgoing.setBounds(267, 429, 60, 26);
            }
            this.add(btnOutgoing);
        }
        return btnOutgoing;
    }

    public void setBtnOutgoing(FactionBtn btnOutgoing) {
        this.btnOutgoing = btnOutgoing;
    }

    public FactionBtn getBtnKick() {
        if (btnKick == null) {
            if("水墨UI".equals(MyIsif.getStyle())) {
                btnKick = new FactionBtn("skin/8100226/newbutton/B22.png", 1, UIUtils.COLOR_WHITE2, "踢出帮派", UIUtils.TEXT_HY16, 12,
                        this);
                btnKick.setBounds(330, 429, 99, 24);

            }else{
                btnKick = new FactionBtn("inkImg/hongmu/1/1_png.button.btn_xy.png", 1, UIUtils.COLOR_BTNPUTONG2, "踢出帮派", UIUtils.TEXT_HY88, 12,
                        this);
                btnKick.setBounds(330, 429, 80, 26);
            }
            this.add(btnKick);
        }
        return btnKick;
    }

    public void setBtnKick(FactionBtn btnKick) {
        this.btnKick = btnKick;
    }

    public FactionBtn getBtnBreak() {
        if (btnBreak == null) {
            if("水墨UI".equals(MyIsif.getStyle())) {

                btnBreak = new FactionBtn("skin/8100226/newbutton/B22.png", 1, UIUtils.COLOR_WHITE2, "脱离帮派", UIUtils.TEXT_HY16,
                        13, this);
                btnBreak.setBounds(433, 429, 99, 24);
            }else{
                btnBreak = new FactionBtn("inkImg/hongmu/1/1_png.button.btn_xy.png", 1, UIUtils.COLOR_BTNPUTONG2, "脱离帮派", UIUtils.TEXT_HY88,
                        13, this);
                btnBreak.setBounds(433, 429, 80, 26);
            }
            this.add(btnBreak);
        }
        return btnBreak;
    }

    public void setBtnBreak(FactionBtn btnBreak) {
        this.btnBreak = btnBreak;
    }

    public FactionBtn getBtnAddFriend() {
        if (btnAddFriend == null) {
            if("水墨UI".equals(MyIsif.getStyle())) {

                btnAddFriend = new FactionBtn("skin/8100226/newbutton/B22.png", 1, UIUtils.COLOR_WHITE2, "加为好友",
                        UIUtils.TEXT_HY16, 14, this);
                btnAddFriend.setBounds(536, 429, 99, 24);
            }else{
                btnAddFriend = new FactionBtn("inkImg/hongmu/1/1_png.button.btn_xy.png", 1, UIUtils.COLOR_BTNPUTONG2, "加为好友",
                        UIUtils.TEXT_HY88, 14, this);
                btnAddFriend.setBounds(536, 429, 80, 26);
            }
            this.add(btnAddFriend);
        }
        return btnAddFriend;
    }

    public void setBtnAddFriend(FactionBtn btnAddFriend) {
        this.btnAddFriend = btnAddFriend;
    }

    public JLabel getLabCheck() {
        if (labCheck == null) {
            labCheck = new JLabel();
            labCheck.setBounds(50, 437, 17, 16);
            labCheck.setOpaque(false);
            labCheck.addMouseListener(new MouseAdapter() {
                @Override
                public void mousePressed(MouseEvent e) {
                    if (labCheck.getIcon() != null) {
                        labCheck.setIcon(null);
                    } else {
                        labCheck.setIcon(CutButtonImage.getImage("skin/8100226/button/B88.png", -1, -1));
                    }
                    if (menuType != 7) {
                        showMenuMessage(factionCardJpanel.getGangResultBean());
                    }
                }
                @Override
                public void mouseEntered(final MouseEvent e) {
                    MessagrFlagUntil.setMouse(MessagrFlagUntil.MOUSE12);
                }

                @Override
                public void mouseExited(final MouseEvent e) {
                    MessagrFlagUntil.setMouse(MessagrFlagUntil.MOUSE1);
                }
            });
            this.add(labCheck);
        }
        return labCheck;
    }

    public void setLabCheck(JLabel labCheck) {
        this.labCheck = labCheck;
    }

    public FactionCardJpanel getFactionCardJpanel() {
        return factionCardJpanel;
    }

    public void setFactionCardJpanel(FactionCardJpanel factionCardJpanel) {
        this.factionCardJpanel = factionCardJpanel;
    }

}
